package test.java;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

import java.io.File;

public class JettyServer {

    public static final int defaultJettyPort = 8084;

    private int jettyPort = defaultJettyPort;

    public void start() throws Exception {
        Server server = new Server(jettyPort);
        server.setHandler(createWebapp());
        server.start();
        server.join();
    }

    protected WebAppContext createWebapp() {

        WebAppContext webapp = new WebAppContext();
        System.out.println(getWebDescriptor());
        webapp.setDescriptor(getWebDescriptor());
        webapp.setResourceBase(getAppRoot());
        webapp.setContextPath("");

        //  webapp.setParentLoaderPriority(true);
        return webapp;
    }


    public void setJettyPort(int jettyPort) {
        this.jettyPort = jettyPort;
    }

    private String getWebDescriptor() {
        return getAppRoot() + File.separator +
                "WEB-INF" + File.separator + "web.xml";
    }


    protected String getAppRoot() {
        return System.getProperty("user.dir") + File.separatorChar + "fable-web" + File.separatorChar + "src"
                + File.separatorChar + "main" + File.separator + "webapp";
    }

    public static void main(String args[]) throws Exception {
        JettyServer jettyServer = new JettyServer();
        jettyServer.start();

    }


}
